package com.kyub.spring.gears.tags;


import com.kyub.spring.gears.model.*;
import com.kyub.spring.gears.util.IntrospectionUtils;
import com.kyub.spring.gears.PrjLogger;


import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * JSTL helper functions
 */
public class Functions {



    public static Object readProperty(java.lang.Object o, java.lang.String s) {
       PrjLogger.debug(Functions.class,"Property : " + s);
       PrjLogger.debug(Functions.class,"Object : " + o);
        try {
            return IntrospectionUtils.readPropertyValue(o, s);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isSimpleProperty(Object p) {
        return (p instanceof SimplePropertyDescriptor);
    }

    public static boolean isRelation(Object p) {
        return (p instanceof RelationPropertyDescriptor);
    }

    public static boolean isCollection(Object p) {
        return (p instanceof CollectionPropertyDescriptor);
    }

    public static boolean isStructProperty(Object p) {
        return (p instanceof StructPropertyDescriptor);
    }

    public static boolean isFile(Object p) {
        return (p instanceof StructPropertyDescriptor && ((StructPropertyDescriptor) p).getLayout() instanceof FileLayout);
    }

    public static String buildHref(FieldLayout layout, Object p, String basePath) {
        FieldLayoutMetadata layoutMetadata = (FieldLayoutMetadata) layout;

        if (layoutMetadata.getHrefParams() == null)
            return basePath + layoutMetadata.getHrefPattern();
        else {
            StringTokenizer t = new StringTokenizer(layoutMetadata.getHrefParams(), ",");
            Collection<Object> arguments = new ArrayList<Object>();
            while (t.hasMoreElements()) {
                try {
                    arguments.add(IntrospectionUtils.readPropertyValue(p, t.nextElement().toString()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (InvocationTargetException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }

            return basePath + MessageFormat.format(layoutMetadata.getHrefPattern(), arguments.toArray());

        }


    }

    public static boolean isCustomWidget(Object p){
        return (p instanceof StructPropertyDescriptor && ((StructPropertyDescriptor)p).getLayout() instanceof CustomFieldLayout);

    }

}
